﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Devart.Data.PostgreSql;

namespace Sistema_financiero.Entidades
{
    public class Vehiculo
    {
        public int ID { get; set; }
        public int Id_tipo_vehiculo { get; set; }
        public string Tipo_vehiculo { get; set; }
        public string Marca { get; set; }
        public string Modelo { get; set; }
        public int Anio { get; set; }
        public string Dominio { get; set; }

        public static void Guardar_vehiculo(Vehiculo vehiculo, string id_cliente)
        {
            int id_vehiculo;
            using (PgSqlConnection conexion = new PgSqlConnection(frm_Menu.conexion))
            {
                conexion.Unicode = true;
                conexion.Open();
                string sql = String.Format("INSERT INTO \"Vehiculo\" VALUES (default, '" + Convert.ToString(vehiculo.Id_tipo_vehiculo)+"', '"+vehiculo.Modelo+"', '"+vehiculo.Anio+"', '"+vehiculo.Dominio+"','"+vehiculo.Marca+"')");
                PgSqlCommand comando = new PgSqlCommand(sql, conexion);
                PgSqlDataReader reader = comando.ExecuteReader();
                reader.Read();
                id_vehiculo = Obtener_id_vehiculo(vehiculo);
                string sql2 = String.Format("INSERT INTO \"Vehiculo_Cliente\" (\"id_cliente\", \"id_vehiculo\") VALUES ({0}, {1})", id_cliente, id_vehiculo);
                PgSqlCommand comando2 = new PgSqlCommand(sql2, conexion);
                PgSqlDataReader reader2 = comando2.ExecuteReader();
                reader2.Read();

                conexion.Close();


            }
        }

        public static int Obtener_id_vehiculo(Vehiculo vehiculo_)
        {
            int id;
            id = 0;
            List<Vehiculo> lista = new List<Vehiculo>();
            using (PgSqlConnection conexion = new PgSqlConnection(frm_Menu.conexion))
            {

                conexion.Unicode = true;
                conexion.Open();
                string sql = String.Format("SELECT v.id_vehiculo FROM \"Vehiculo\" v WHERE v.id_tipo_vehiculo = '"+Convert.ToString(vehiculo_.Id_tipo_vehiculo) +"' and v.modelo = '"+Convert.ToString(vehiculo_.Modelo) + "' and v.anio = '"+Convert.ToString(vehiculo_.Anio) + "' and v.dominio = '"+ Convert.ToString(vehiculo_.Dominio)+ "' and v.marca = '"+Convert.ToString(vehiculo_.Marca)+"'");
                PgSqlCommand comando = new PgSqlCommand(sql, conexion);
                PgSqlDataReader reader = comando.ExecuteReader();
                while (reader.Read())
                {
                    id = Convert.ToInt32(reader[0]);
                }
                conexion.Close();

            }

            return id;

        }
        
    }
}
